//
// Created by YeMingMing on 2024-03-06.
//

// 链表选择排序


#include "../linklist.h"
#include <stdio.h>
#include <stdlib.h>


void Swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void SelectionSort(LinkList L) {
    LNode *p = L->next;
    while (p != NULL) {
        LNode *min = p;
        LNode *q = p->next;
        while (q != NULL) {
            if (q->data < min->data)
                min = q;
            q = q->next;
        }
        if (min != p)
            Swap(&(min->data), &(p->data));
        p = p->next;
    }
}

int main() {
    LinkList L;
    if (InitList(&L)) {
        InsertElem(L, 3);
        InsertElem(L, 1);
        InsertElem(L, 5);
        InsertElem(L, 2);
        InsertElem(L, 4);
        printf("Original list:\n");
        PrintList(L);
        SelectionSort(L);
        printf("Sorted list:\n");
        PrintList(L);
    } else {
        printf("Failed to initialize the list.\n");
    }
    return 0;
}

